//导入express
const express = require("express")
//引入数据库对象
const connection = require(`${__dirname}/../mysql/index.js`)
//创建路由对象
const router = express.Router()

//登录
/*参数
* account String 必传 账号
* password String 必传 密码*/
router.post("/login",(req,res)=>{
    const {account,password} = req.body
    if(account === undefined || account === "" || password === undefined || password === ""){
        return res.send({code: 1, data: {}, msg: "必传参数缺失"})
    }
    const sql = `select * from user where u_account = "${account}" and u_password = "${password}"`
    connection.query(sql,(err,result)=>{
        if(err){
            console.log(err)
            res.send({code: 1,data: {},msg: "查询失败"})
        }else{
            if(result.length === 0){
                res.send({code: 1,data: {},msg: "账号密码不匹配"})
            }else{
                const data = {
                    uid: result[0].u_id,
                    name: result[0].u_name,
                    account: result[0].u_account,
                    wid: result[0].w_id,
                    color: result[0].u_color,
                    bg: result[0].u_bg,
                    primary: result[0].u_primary,
                    header: result[0].u_header
                }
                res.send({code: 0,data: {userInfo: data},msg: "查询成功"})
            }
        }
    })
})

//注册
/*参数
* account String 必传 账号
* password String 必传 密码
* problem String 可选 密保问题
* answer String 若传递了problem则必传 密保答案*/
router.post("/register",(req,res)=>{
    const {account,password,problem,answer} = req.body
    if(account === undefined || account === "" || password === undefined || password === ""){
        return res.send({code: 1, data: {}, msg: "必传参数缺失"})
    }
    //判断账号是否存在
    connection.query(`select * from user where u_account = "${account}"`,(err,result)=>{
        if(err){
            return res.send({code: 1, data: {}, msg: "服务器异常"})
        }else if(result.length > 0){
            return res.send({code: 1,data: {},msg: "该账号已经存在"})
        }
        //设置sql语句
        let sql = `insert into user(u_account,u_password) values("${account}","${password}")`
        //判断是否设置密保
        if(problem !== undefined && problem !== ""){
            if(answer === undefined || answer === ""){
                return res.send({code: 1, data: {}, msg: "必传参数缺失"})
            }else{
                //修改为有密保的sql语句
                sql = `insert into user(u_name,u_account,u_password,u_problem,u_answer,u_header) 
                    values("${account}","${account}","${password}","${problem}","${answer}","${req.protocol}://${req.headers.host}/user/default.jpg")`
            }
        }
        //执行注册操作
        connection.query(sql,err=>{
            if(err){
                console.log(err)
                res.send({code: 1,data: {},msg: "注册失败"})
            }else{
                res.send({code: 0,data: {},msg: "注册成功"})
            }
        })
    })
})

//查询
/*参数
* uid int 可选 用户id
* name String 可选 用户名
* 若无参数则返回全部用户*/
router.post("/get_user",(req,res)=>{
    const {uid,name} = req.body
    const sql = `select * from user where 1 = 1 
        ${uid ? `and u_id = ${uid}` : ""} 
        ${name ? `and u_name like "%${name}%"` : ""}`
    connection.query(sql,(err,result)=>{
        if(err){
            console.log(err)
            res.send({code: 1,data: {},msg: "查询失败"})
        }else{
            const userList = []
            result.map(item=>{
                const data = {
                    uid: item.u_id,
                    name: item.u_name,
                    account: item.u_account,
                    wid: item.w_id,
                    color: item.u_color,
                    bg: item.u_bg,
                    primary: item.u_primary,
                    header: item.u_header
                }
                userList.push(data)
            })
            res.send({code: 0,data: {userList},msg: "查询成功"})
        }
    })
})

//找回密码——获取问题
/*参数
* account String 必传 账号*/
router.post("/get_problem",(req,res)=>{
    const {account} = req.body
    if(account === undefined || account === ""){
        return res.send({code: 1, data: {}, msg: "必传参数缺失"})
    }
    const sql = `select u_problem from user where u_account = "${account}"`
    connection.query(sql,(err,result)=>{
        if(err){
            console.log(err)
            return res.send({code: 1,data: {},msg: "服务器异常"})
        }
        res.send({code: 0,data: {problem: result[0].u_problem},msg: "查询成功"})
    })
})

//找回密码——获取密码
/*参数
* account String 必传 账号
* answer String 必传 密保答案*/
router.post("/get_password",(req,res)=>{
    const {account,answer} = req.body
    if(account === undefined || account === "" || answer === undefined || answer === ""){
        return res.send({code: 1, data: {}, msg: "必传参数缺失"})
    }
    const sql = `select u_password from user where u_account = "${account}" and u_answer = "${answer}"`
    connection.query(sql,(err,result)=>{
        if(err){
            console.log(err)
            return res.send({code: 1,data: {},msg: "服务器异常"})
        }
        res.send({code: 0,data: {password: result[0].u_password},msg: "查询成功"})
    })
})

//修改密码
/*参数
* uid int 必传 用户id
* account String 必传 用户账号
* password String 必传 旧密码
* newPassword String 必传 新密码*/
router.post("/update_password",(req,res)=>{
    const {uid,account,password,newPassword} = req.body
    if(
        uid === undefined || uid === "" ||
        account === undefined || account === "" ||
        password === undefined || password === "" ||
        newPassword === undefined || newPassword === ""
    ){
        return res.send({code: 1, data: {}, msg: "必传参数缺失"})
    }
    const sql = `update user set u_password = "${newPassword}"
                where u_id = ${uid} 
                and u_account = "${account}"
                and u_password = "${password}"`
    connection.query(sql,err=>{
        if(err){
            console.log(err)
            return res.send({code: 1,data: {},msg: "修改失败"})
        }
        res.send({code: 0,data: {},msg: "修改成功"})
    })
})

//修改用户信息
/*参数
* uid int 必传 用户id
* name String 可选 修改后的用户名
* wid int 可选 修改后的小部件id
* color String 可选 修改后的字体颜色
* bg String 可选 修改后的背景颜色
* primary String 可选 修改后的激活状态字体颜色
* header String 可选 修改后的用户头像路径*/
router.post("/update_user",(req,res)=>{
    const {uid,name,wid,color,bg,primary,header} = req.body
    if(uid === undefined || uid === ""){
        return res.send({code: 1, data: {}, msg: "必传参数缺失"})
    }
    const sql = `update user set 
                u_id = ${uid}
                ${name ? `,u_name = "${name}"` : ""}
                ${wid ? `,w_id = ${wid}` : ""}
                ${color ? `,u_color = "${color}"` : ""}
                ${bg ? `,u_bg = "${bg}"` : ""}
                ${primary ? `,u_primary = "${primary}"` : ""}
                ${header ? `,u_header = "${header}"` : ""}
                where u_id = ${uid}`
    connection.query(sql,err=>{
        if(err){
            console.log(err)
            return res.send({code: 1,data: {},msg: "修改失败"})
        }
        res.send({code: 0,data: {},msg: "修改成功"})
    })
})

module.exports = router